package com.quiz.source;

import com.quiz.source.database.QuizDatabaseHelper;
import com.quiz.source.database.QuizDbAdapter;
import com.quiz.source.model.details;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

@SuppressWarnings("unused")
public class insert extends Activity{
	
	private EditText mcategory;
    private EditText mquestion;
    private EditText moption1;
    private EditText moption2;
    private EditText moption3;
    private EditText moption4;
    private EditText manswer;
    private Long mRowId;
    private QuizDbAdapter mDbHelper;
   // private Spinner mCategory;
    private details mTodo = null;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        mDbHelper = new QuizDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.insert);
        mcategory = (EditText) findViewById(R.id.category);
        mquestion = (EditText) findViewById(R.id.question);
        moption1 = (EditText) findViewById(R.id.option1);
        moption2 = (EditText) findViewById(R.id.option2);
        moption3 = (EditText) findViewById(R.id.option3);
        moption4 = (EditText) findViewById(R.id.option4);
        manswer = (EditText) findViewById(R.id.answer);
        

        Button confirmButton = (Button) findViewById(R.id.insert);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        mRowId = (bundle == null) ? null : (Long) bundle.getSerializable(QuizDbAdapter.KEY_ROWID);
        if (extras != null) {
            mRowId = extras.getLong(QuizDbAdapter.KEY_ROWID);
        }
        populateFields();
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                setResult(RESULT_OK);
                finish();
            }

        });
    }

    private void populateFields() {

        if (mRowId != null) {
            Cursor todo = mDbHelper.fetchquiz(mRowId);
            startManagingCursor(todo);
            //String category = todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_CATEGORY));

           

            mcategory.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_CATEGORY)));
            mquestion.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_QUESTION)));
            moption1.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_OPTION1)));
            moption2.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_OPTION2)));
            moption3.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_OPTION3)));
            moption4.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_OPTION4)));
            manswer.setText(todo.getString(todo.getColumnIndexOrThrow(QuizDbAdapter.KEY_ANSWER)));
        }
    }

    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(QuizDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {

        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {

        super.onResume();
        populateFields();
    }

    private void saveState() {

        mTodo = new details();
        mTodo.category = (String) mcategory.getText().toString();
        mTodo.question = mquestion.getText().toString();
        mTodo.option1 = moption1.getText().toString();
        mTodo.option2 = moption2.getText().toString();
        mTodo.option3 = moption3.getText().toString();
        mTodo.option4 = moption4.getText().toString();
        mTodo.answer = manswer.getText().toString();

        if (mRowId == null) {
            long id = mDbHelper.createquiz(mTodo);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updatequiz(mRowId, mTodo);
        }
    }

}
